<?php
/**
 * Created by PhpStorm.
 * User: keis
 * Date: 2016/5/23
 * Time: 11:28
 */
require_once('./response.php');
require_once('../db/db.php');

//获取那个类型的数据
$type='';
switch($_GET['type']){

    case 1:$type="新闻";break;
    case 2:$type="军事";break;
    case 3:$type="娱乐";break;
    case 4:$type="笑话";break;

}

$page = isset($_GET['page']) ? $_GET['page'] : 1;
$pageSize = isset($_GET['pagesize']) ? $_GET['pagesize'] : 20;
if(!is_numeric($page) || !is_numeric($pageSize)) {
    echo Response::show(401, '数据不合法');
}

$offset = ($page - 1) * $pageSize;
$sql = "select * from news where type_new = '$type' order by ctime desc limit ". $offset ." , ".$pageSize;

$news = array();//保存数据
try {
    $connect = Db::getInstance()->connect();
} catch(Exception $e) {
    // $e->getMessage();
    echo Response::show(403, '数据库链接失败');
}
$result = mysqli_query($connect,$sql);

if($result){

    while($new = mysqli_fetch_assoc($result)) {
        $news[] = $new;
    }
}

if($news) {
    echo Response::show(200, '数据获取成功', $news);
} else {
    echo Response::show(400, '数据获取失败', $news);
}